<?php
namespace app\shop\controller;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\shop\model\AdminShopType as ShopTypeModel;
class AdminShopType extends AdminRest
{

    protected $model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new ShopTypeModel();
    }
    /**
     * 获取分类列表
     */
    public function cateList(){
        $dis[]= ['uniacid','=',$this->_uniacid];
        $data = $this->model->cateList($dis);
        return $this->success($data);
    }
    /**
     * 上下架分类
     */
    public function cateStatusUpdate(){

        $id     = $this->_input['id'];
        $status = $this->_input['status'];
        if($status==0){
            $son_id = $this->model->getSonCatId($id);
            $dis[]  = ['id','in',$son_id];
        }else{
            $dis[]  = ['id','=',$id];
        }
        $data = ['status'=>$status];

        $dis[]= ['status','>',-1];

        $res  = $this->model->cateUpdate($dis,$data);
        return $this->success($res);
    }

    /**
     * 修改分类
     */
    public function cateUpdate(){
        $input = $this->_input;
        $dis   = ['id'=>$input['id']];
        if(count($input)>1){
            $type   = $this->model->cateInfo($dis);
            if($type['pid']==0&&$input['pid']!=0){
                return $this->error('一级分类不能有父级分类');
            }
            $input['cover'] = implode(',',$input['cover']);
            $res  = $this->model->cateUpdate($dis,$input);
        }else{
            $res  = $this->model->cateInfo($dis);
        }
        return $this->success($res);
    }

    /**
     * 添加分类
     */
    public function cateAdd(){
        $this->_input['uniacid'] = $this->_uniacid;
        $this->_input['cover']   = implode(',',$this->_input['cover']);
        $res  = $this->model->cateAdd($this->_input);
        return $this->success($res);
    }

    /**
     * 删除分类
     */

    public function cateDelete(){
        $input  = $this->_input;
        $son_id = $this->model->getSonCatId($input['id']);
        $dis[]  = ['id','in',$son_id];
        $data   = [
            'status' => -1
        ];
        $res    = $this->model->cateUpdate($dis,$data);
        return $this->success($res);

    }

    /**
     * 获取所有一级分类
     */
    public function cateTop(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['pid','=',0];
        $data  = $this->model->catSelect($dis);
       return  $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-02 11:11
     * @功能说明:diy用的
     */
    public function cateInfoPage(){

        $input= $this->_param;

        $keyword = $input['keyword'];


        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $where = [];

        if ($keyword) {

            $where[] = ['title', 'like', "%{$keyword}%"];
        }


        $data = Db::name('longbing_card_shop_type')->where($dis)->where($where)->field('title,id')->order('id desc')->paginate($input['page_count'])->toArray();

        return $this->success($data);

    }





}
